package com.taoyeshu.modules.sys.utils;

import java.util.List;

import com.taoyeshu.common.utils.TreeObject;
import com.taoyeshu.common.utils.ZTreeObject;
import com.taoyeshu.modules.sys.entity.Resource;

public class TreeUtil {
	/**
	 * 封装treeObject
	 * 
	 * @param list
	 *            分类表
	 * @param typeId
	 *            传入的父节点ID
	 * @return String
	 */
	public TreeObject[] getTreeObjects(List<Resource> list) {
		TreeObject[] trees = new TreeObject[list.size()];
		for (int i = 0; i < trees.length; i++) {
			TreeObject object = new TreeObject();
			Resource resource = list.get(i);
			object.setId(resource.getId());
			object.setName(resource.getName());
			object.setHref(resource.getHref());
			object.setDesc(resource.getDescription());
			object.setType(resource.getType() + "");
			object.setDisplay(resource.getDisplay() + "");
			object.setPermission(resource.getPermission());
			if (resource.getParent() != null && resource.getParent().getId() != null) {
				object.setParent(resource.getParent().getId() + "");
			} else {
				object.setParent(0 + "");
			}
			if (resource.getType() == 0) {
				object.setLevel("0");
				object.setLeaf(false);
				object.setExpanded(true);
				object.setLoaded(false);
			} else if (resource.getType() == 1) {
				object.setLevel("1");
				object.setLeaf(false);
				object.setExpanded(true);
				object.setLoaded(false);
			} else {
				object.setLevel("2");
				object.setLeaf(true);
				object.setExpanded(true);
				object.setLoaded(true);
			}

			trees[i] = object;
		}
		return trees;
	}

	public ZTreeObject[] getZTreeObjects(List<Resource> list, List<Resource> listByRoleId) {
		ZTreeObject[] trees = new ZTreeObject[list.size()];
		for (int i = 0; i < trees.length; i++) {
			ZTreeObject object = new ZTreeObject();
			Resource resource = list.get(i);
			object.setId(resource.getId());
			object.setName(resource.getName());
			if (resource.getParent() != null && resource.getParent().getId() != null) {
				object.setPid(resource.getParent().getId());
			} else {
				object.setPid(0);
			}
			if (resource.getType() == 0) {
				object.setOpen(true);
			} else if (resource.getType() == 1) {
				object.setOpen(true);
			} else {
				object.setOpen(false);
			}

			for (Resource obj : listByRoleId) {
				if (object.getId().equals(obj.getId())) {
					object.setChecked(true);
				}
			}

			trees[i] = object;
		}
		return trees;
	}
}
